package com.wwz.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wwz.mapper.CustomerMapper;
import com.wwz.pojo.Customer;
import com.wwz.pojo.query.CustomerQuery;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 客户接口实现
 * @date 2022/03/26 51:11:05
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements ICustomerService {

    @Override
    public IPage<Customer> toPage(CustomerQuery query) {

        LambdaQueryWrapper<Customer> wrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotEmpty(query.getName())){
            wrapper.like(Customer::getName,query.getName());
        }
        wrapper.orderByDesc(Customer::getCreateTime);

        return this.page(query.toPage(), wrapper);
    }

    @Override
    public void addOrUpdate(Customer customer) {
        Customer byId = getById(customer);

        if (byId == null){
            customer.setId(StrUtil.uuid());
            customer.setCreateTime(new Date());
            save(customer);
            return;
        }
        updateById(customer);
    }
}
